<?php

/* ----------------------------------------------
  Caloris: Module File Manager

  $Id$
  $Author$

  File Manager, složka cal-bin a její podsložky
  ---------------------------------------------- */

use Nette\Image;

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'filemanager');

$formFileUpload = new Nette\Forms\Form;
$formFileUpload->setAction(_CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/filemanager/index.php');
$formFileUpload->getElementPrototype()->class = 'form-horizontal';
$formFileUpload->addHidden('function_name', 'files_upload');
$formFileUpload->addHidden('id', $_REQUEST["id"]);
$formFileUpload->addUpload('the_file', 'Soubor:')
        ->addRule(Nette\Forms\Form::MIME_TYPE, 'Vložený obrázek má nesprávný formát (povolené formáty: JPEG)', array('image/png', 'image/jpeg', 'image/jpg', 'image/gif'));
$formFileUpload->addCheckbox('thumb', "Vytvořit thumbnail")
        ->setDefaultValue('CHECKED');
$formFileUpload->addSubmit('send', 'Přihlásit');

if ($_REQUEST["function_name"] == 'files_delete') {
    $filePath = _CALSET_PATHS_BASE . '/cal-bin/pages/' . $_POST["filename"];

    // Upload souboru
    if (file_exists($filePath)) {
        unlink($filePath);
    }

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
        exit();
    }
} elseif ($formFileUpload->isSuccess() && $_REQUEST["function_name"] == 'files_upload') {
    $formFileUploadValues = $formFileUpload->getValues();

    $filePath = _CALSET_PATHS_BASE . '/cal-bin/pages/' . $formFileUploadValues->the_file->name;
    $filePathThumb = _CALSET_PATHS_BASE . '/cal-bin/pages/tn_' . $formFileUploadValues->the_file->name;

    if (file_exists($filePath)) {
        $referer = 'http://' . parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST) . parse_url($_SERVER["HTTP_REFERER"], PHP_URL_PATH) . '?msg=' . urlencode("Soubor již existuje");
        header("location:" . $referer);
        exit();
    }

    if (file_exists($filePathThumb)) {
        $referer = 'http://' . parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST) . parse_url($_SERVER["HTTP_REFERER"], PHP_URL_PATH) . '?msg=' . urlencode("Náhled již existuje");
        header("location:" . $referer);
        exit();
    }

    copy($_FILES["the_file"]["tmp_name"], $filePath);
    chmod($filePath, 0777);

    if ($formFileUploadValues->thumb == true) {
        // Thumbnails set to 200 max width
        $image = Image::fromFile(_CALSET_PATHS_BASE . '/cal-bin/pages/' . $formFileUploadValues->the_file->name);
        $image->resize(200, 150, Nette\Image::EXACT);
        $image->sharpen();
        $image->save(_CALSET_PATHS_BASE . '/cal-bin/pages/t200_' . $formFileUploadValues->the_file->name);
    }

    header("location: " . $_SERVER["HTTP_REFERER"] . "");
    exit();
}

$content = '<h1>Souborový manažer</h1>';

if ($_REQUEST["msg"] != '') {
    $content .= '<p style="color: red;">' . $_REQUEST["msg"] . '</p>';
}

$content .= '
<h2>Upload souboru</h2>' . PHP_EOL;

$content .= $formFileUpload;

$content .= '
<h2>Seznam souborů</h2>' . PHP_EOL;

$dirscan = scandir(_CALSET_PATHS_BASE . '/cal-bin/pages');

$content .= '
<table class="table table-striped">
    <tr>
        <th>&nbsp;</th>
        <th>Náhled</th>
        <th>Název</th>
        <th>Velikost (v kB)</th>
    </tr>';

for ($a = 0; $a < count($dirscan); $a++) {
    if ($dirscan[$a] === '.' || $dirscan[$a] === '..') {
        continue;
    } elseif(substr($dirscan[$a],0,5) == 't200_') {
        continue;
    } else {
        $content .= '
    <tr id="trba_' . $a . '" >
        <td>
<form id="formshw_' . $a . '" action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/filemanager/index.php" name="trba_' . $a . '" method="post" onsubmit="return false;">
<input type="hidden" name="function_name" value="files_delete"  />
<input type="hidden" name="filename" value="' . $dirscan[$a] . '"  />
<input type="submit" id="formx_' . $a . '" value="Smazat" class="btn btn-danger" />
</form>
        </td>
        <td>';
        
        if (file_exists(_CALSET_PATHS_BASE . '/cal-bin/pages/t200_' . $dirscan[$a])) {
            $content .= '<a href="' . _CALSET_PATHS_URI . '/cal-bin/pages/' . $dirscan[$a] . '"><img src="' . _CALSET_PATHS_URI . '/cal-bin/pages/t200_' . $dirscan[$a] . '" /></a>';
        }
        
        $content .= '</td>
        <td><a href="' . _CALSET_PATHS_URI . '/cal-bin/pages/' . $dirscan[$a] . '">' . $dirscan[$a] . '</a></td>
        <td>' . round(filesize(_CALSET_PATHS_BASE . '/cal-bin/pages/' . $dirscan[$a]) / 1024, 2) . ' kB</td>
    </tr>';
    }
}

$content .= '
</table>' . PHP_EOL;

$data["title"] = 'Souborový manažer';

$tmpl = new Caloris\Template;
$tmpl->input(array($content));
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');